#!/usr/bin/env node

'use strict';

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const utils = require('../utils.js');

assert(!module.parent);
assert(__dirname === process.cwd());

const target = process.argv[2] || 'host';
const input = './test-x-index.js';
const output = './test-output.exe';

let right;

utils.pkg.sync(['--target', target, '--output', output, input]);

let spoiler = fs.readFileSync(output);
spoiler = Buffer.concat([
  spoiler,
  Buffer.from([
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
    0xfe,
    0x01,
    0x12,
    0x23,
    0x34,
    0x45,
    0x56,
    0x67,
    0x78,
    0x89,
    0x9a,
    0xab,
    0xbc,
    0xcd,
    0xde,
    0xef,
  ]),
]);
fs.writeFileSync(output, spoiler);

right = utils.spawn.sync('./' + path.basename(output), [], {
  cwd: path.dirname(output),
});

assert.strictEqual(right, 'ok\n');
utils.vacuum.sync(output);
